package com.fcscs.cservice.client.main.configuration.iguest;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.fcscs.core.common.authority.CServiceAuthority;
import com.fcscs.core.common.authority.UserAuthority;
import com.fcscs.core.domain.IdNameModel;
import com.fcscs.core.domain.Property;
import com.fcscs.core.gwt.common.AsyncCallbackListener;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.SessionContext;
import com.fcscs.core.gwt.common.ui.CenteredInfo;
import com.fcscs.core.gwt.common.ui.FcsFields.EasyClearCombobox;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.FcsFields.HighLightIconButton;
import com.fcscs.core.gwt.common.ui.FcsPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.MultiLanguageConstants;
import com.fcscs.cservice.client.common.controller.CServiceCommonServiceController;
import com.fcscs.cservice.client.common.model.configuration.reservation.PropertyIdNameModelData;
import com.fcscs.cservice.client.common.model.iguestusers.iGuestUsersModel;
import com.fcscs.cservice.client.common.model.iguestusers.iGuestUsersModelData;
import com.fcscs.cservice.client.main.configuration.iguest.iGuestLoginOptionDialog.LoginOption;
import com.fcscs.cservice.client.main.ui.DialogEvent;
import com.fcscs.cservice.client.main.ui.DialogEventHandler;
import com.fcscs.cservice.client.main.ui.PrecheckinDialogEvent;
import com.fcscs.cservice.domain.AuthenticationConfig;
import com.fcscs.cservice.domain.ConstantsLanguageValue;
import com.fcscs.cservice.domain.ConstantsLanguageValueId;
import com.fcscs.cservice.domain.DedicatedVideoConfig;
import com.fcscs.cservice.domain.IGuestUserView;
import com.fcscs.share.gwt.common.ShareGWTConstants;
import com.fcscs.share.gwt.common.ui.MultiLanguageDialog;
import com.fcscs.share.gwt.common.ui.MultiLanguageTextArea;
import com.fcscs.share.gwt.common.ui.event.MultiLanguageDialogEventHandler;
import com.fcscs.share.gwt.common.ui.event.MultiLanguageDialogOKEvent;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;

public class iGuestAuthenConfigPanel extends FcsPanel implements
		DialogEventHandler {
	private VerticalPanel vp;
	private TextField<String> reservationSearchDaysField;

	private EasyClearCombobox<PropertyIdNameModelData> propCombox;
	private ListStore<PropertyIdNameModelData> propListStore;
	private List<IdNameModel<Property>> propGroupIdNames;

	private TextField<String> loginopTextField;
	private TextField<String> eUsernameTextField;
	private TextField<String> passwordTextField;
	private TextField<String> precheckinTextField;
	private TextField<String> levelTextField;
	private TextField<String> netTextField;
	private TextField<String> loginpTextField;
	private TextField<String> loginiTextField;
	private TextField<String> serviceTextField;
	private  MultiLanguageDialog netDialog;
	private  MultiLanguageDialog loginpDialog;
	private  MultiLanguageDialog loginiDialog;
	private  MultiLanguageDialog serviceDialog;
	public FieldSet fieldSet1;
	private Label l2;
	private AuthenticationConfig currentAuthenConf;
	private Map<String, String> networkAccessNotAvailableConf;
	private Map<String, String> loginPrompt;
	private Map<String, String> loginInvalid;
	private Map<String, String> serviceNotAvailablePrecheckin;
	private ConstantsLanguageValueId constants;
	private PropertyIdNameModelData currentProperty;
	private iGuestLoginOptionDialog dialog = iGuestLoginOptionDialog
			.getInstance();
	private PrecheckinDialog predialog = PrecheckinDialog.getInstance();

	// private iGuestUsersModelData currentTestModel;

	public iGuestAuthenConfigPanel(String id) {
		super(id);
		dialog.addHandler(this, DialogEvent.TYPE);
		predialog.addHandler(this, PrecheckinDialogEvent.TYPE);
	}

	
	@Override
	public void init() {
		vp = new VerticalPanel();
		vp.setId("PropertyConfigPanel_vpPanel");
		vp.setSpacing(5);

		LayoutContainer lc = new LayoutContainer();
		lc.setId("PropertyConfigPanel_lc");
		lc.setLayout(new ColumnLayout());

		Label propLabel = new Label(CoreConstants.CORE_CONSTANTS.property()
				+ CoreConstants.REQURIED_FIELD_LABEL);
		propLabel.setId("PropertyConfigPanel_propLabel");

		propListStore = new ListStore<PropertyIdNameModelData>();
		propCombox = new EasyClearCombobox<PropertyIdNameModelData>();
		propCombox.setId("PropertyConfigPanel_propCombox");
		// propCombox.setFieldLabel(CoreConstants.CORE_CONSTANTS.property());
		propCombox.setWidth(200);
		propCombox.setDisplayField(CoreConstants.FULLTNAME);
		propCombox.setAllowBlank(false);
		propCombox.setForceSelection(true);
		propCombox.setTriggerAction(TriggerAction.ALL);
		propCombox.setStore(propListStore);
		propCombox
				.addSelectionChangedListener(new SelectionChangedListener<PropertyIdNameModelData>() {
					@Override
					public void selectionChanged(
							SelectionChangedEvent<PropertyIdNameModelData> se) {
						// myClear();
						// CServiceCommonServiceController.getINSTANCE().getAuthen(
						// propCombox.getValue().getEntity().getId(),
						// new AsyncCallbackListener<AuthenticationConfig>(){
						// @Override
						// public void onLogin(String username) {
						// CServiceCommonServiceController.getINSTANCE().getAuthen(
						// propCombox.getValue().getEntity().getId(),this);
						// }
						//
						// @Override
						// public void onFailure(Throwable caught) {
						// MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.system(),
						// ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
						// CoreConstants.CORE_CONSTANTS.systemError(), null);
						// }
						//
						// @Override
						// public void onSuccess(AuthenticationConfig result) {
						// if(null != result){
						// // currentPropConf = result;
						// //reservationSearchDaysField.setValue(result.getReservationSearchDays()
						// + "");
						// }
						// //currentAuthenConf.setLoginOption(loginopTextField.getValue()
						// == null ? null : loginopTextField.getValue().trim());
						// currentAuthenConf.setUsername(eUsernameTextField.getValue()
						// == null ? null :
						// eUsernameTextField.getValue().trim());
						// currentAuthenConf.setPassword(passwordTextField.getValue()
						// == null ? null :
						// passwordTextField.getValue().trim());
						// }
						//
						// });
					}
				});

		lc.add(propLabel, new ColumnData(70));
		lc.add(propCombox, new ColumnData(160));

		vp.add(lc);

		FormPanel panel = new FormPanel();
		panel.setId("RCConfigPanel_panel");
		panel.setFrame(true);
		panel.setHeading(CServiceConstants.CSERVICE_CONSTANTS.iguestauthen());
		panel.setPadding(10);
		panel.setSize(730,450);
		panel.setLabelAlign(LabelAlign.LEFT);
		panel.setLabelSeparator("");
		panel.setLabelWidth(200);
		panel.setFieldWidth(400);

		// Them LayoutContainer Login Option
		VerticalPanel lca = new VerticalPanel();
		lca.setBorders(true);
		lca.setSpacing(5);

		HorizontalPanel lcab = new HorizontalPanel();
		Label l = new Label(CServiceConstants.CSERVICE_CONSTANTS.loginoption());
		l.setStyleAttribute("marginLeft", "5px");
		lcab.add(l);

		l2 = new Label(CoreConstants.CORE_CONSTANTS.username());
		l2.setStyleAttribute("marginLeft", "80px");
		lcab.add(l2);

		Label l3 = new Label(CoreConstants.CORE_CONSTANTS.pwd());
		l3.setStyleAttribute("marginLeft", "80px");
		lcab.add(l3);

		// khai bao lcb chua trong lca
		HorizontalPanel lcb = new HorizontalPanel();
		lcb.setId("InterfaceConfigPanel_lcb");
		lcb.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "5px");
		lcb.setStyleAttribute(CServiceConstants.CSS_MARGINBUTTOM, "10px");

		loginopTextField = new TextField<String>();
		loginopTextField.setWidth(150);
		loginopTextField.setReadOnly(true);
		loginopTextField.setId("InterfaceConfigPanel_eWebServiceURLTextField");
		// loginopTextField.set;
		loginopTextField.setValidator(new Validator() {
			@Override
			public String validate(Field<?> field, String value) {
				if (value != null && !value.trim().isEmpty()
						&& value.trim().length() > 255) {
					return ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(
							CServiceConstants.CSERVICE_CONSTANTS
									.webServiceURL(), 200);
				}
				return null;
			}
		});

		lcb.add(loginopTextField);

		eUsernameTextField = new TextField<String>();
		eUsernameTextField.setId("InterfaceConfigPanel_eUsernameTextField");
		eUsernameTextField.setWidth(100);
		eUsernameTextField.setReadOnly(true);
		eUsernameTextField.setStyleAttribute("marginLeft", "5px");
		eUsernameTextField.setValidator(new Validator() {
			@Override
			public String validate(Field<?> field, String value) {
				if (value != null && !value.trim().isEmpty()
						&& value.trim().length() > 255) {
					return ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(
							CoreConstants.CORE_CONSTANTS.username(), 100);
				}
				return null;
			}
		});
		lcb.add(eUsernameTextField);

		passwordTextField = new TextField<String>();
		passwordTextField.setWidth(100);
		passwordTextField.setReadOnly(true);
		passwordTextField.setStyleAttribute("marginLeft", "5px");
		passwordTextField.setId("InterfaceConfigPanel_passwordTextField");
		passwordTextField.setValidator(new Validator() {
			@Override
			public String validate(Field<?> field, String value) {
				if (value != null && !value.trim().isEmpty()
						&& value.trim().length() > 255) {
					return ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(
							CoreConstants.CORE_CONSTANTS.pwd(), 100);
				}
				return null;
			}
		});
		lcb.add(passwordTextField);
		// Tao nut edit
		HighLightIconButton ib = createEditBtn();

		lcb.add(ib);
		lca.add(lcab);
		lca.add(lcb);

		panel.add(lca);

		// Them Pre Check-in

		VerticalPanel lcpre1 = new VerticalPanel();
		lcpre1.setSpacing(5);
		lcpre1.setBorders(true);
		lcpre1.setSpacing(5);
		lcpre1.setStyleAttribute(CServiceConstants.CSS_MARGINTOP, "15px");

		HorizontalPanel lcpre = new HorizontalPanel();
		lcpre.setId("InterfaceConfigPanel_lcpre");

		Label pre = new Label(CServiceConstants.CSERVICE_CONSTANTS.precheckin());
		pre.setStyleAttribute("marginLeft", "5px");
		lcpre.add(pre);

		Label level = new Label(CServiceConstants.CSERVICE_CONSTANTS.level());
		level.setStyleAttribute("marginLeft", "50px");
		lcpre.add(level);

		// // lcpre2 chua trong lcpre
		HorizontalPanel lcprex = new HorizontalPanel();
		lcprex.setId("InterfaceConfigPanel_lcpre2");
		lcprex.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "5px");
		lcprex.setStyleAttribute(CServiceConstants.CSS_MARGINBUTTOM, "10px");

		precheckinTextField = new TextField<String>();
		precheckinTextField.setWidth(100);
		precheckinTextField.setReadOnly(true);
		precheckinTextField.setId("InterfaceConfigPanel_precheckinTextField");
		precheckinTextField.setValidator(new Validator() {
			@Override
			public String validate(Field<?> field, String value) {
				if (value != null && !value.trim().isEmpty()
						&& value.trim().length() > 255) {
					return ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(
							CServiceConstants.CSERVICE_CONSTANTS
									.webServiceURL(), 80);
				}
				return null;
			}
		});

		lcprex.add(precheckinTextField);

		levelTextField = new TextField<String>();
		levelTextField.setWidth(100);
		levelTextField.setReadOnly(true);
		levelTextField.setStyleAttribute("marginLeft", "5px");
		levelTextField.setId("InterfaceConfigPanel_levelTextField");
		levelTextField.setValidator(new Validator() {
			@Override
			public String validate(Field<?> field, String value) {
				if (value != null && !value.trim().isEmpty()
						&& value.trim().length() > 255) {
					return ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(
							CServiceConstants.CSERVICE_CONSTANTS
									.webServiceURL(), 80);
				}
				return null;
			}
		});

		lcprex.add(levelTextField);
		HighLightIconButton ic = createPreEditBtn();
		lcprex.add(ic);
		lcpre1.add(lcpre);
		lcpre1.add(lcprex);
		panel.add(lcpre1);

		// Them LayoutContainer Popup Configuration
		fieldSet1 = new FieldSet();
		fieldSet1.setWidth(700);
		fieldSet1.setBorders(true);
		fieldSet1.setId("RestaurantConfigPanel_fieldSet1");
		fieldSet1.setStyleAttribute(CServiceConstants.CSS_MARGINTOP, "15px");
		fieldSet1.setHeading(CServiceConstants.CSERVICE_CONSTANTS.popupconfig()
				+ CoreConstants.REQURIED_FIELD_LABEL);

		Label lnet = new Label(CServiceConstants.CSERVICE_CONSTANTS.networknotaccess());
		//lnet.set
		lnet.setStyleAttribute("marginButtom", "5px");

		// /MultiLanguage
		//final Label dialogTestValueLabel = new Label("Value");
		netDialog = new MultiLanguageDialog();
		netDialog.setAllowBlank(false);
		netDialog.addMultiLanguageDialogEventHandler(new MultiLanguageDialogEventHandler() {

					@Override
					public void onButtonOKClicked(
							MultiLanguageDialogOKEvent event) {
						// Get map language-text
						Map<String, String> values = event.getSource().getLanguageTexts();

						// Display English in label
						netTextField.setValue(values.get(MultiLanguageDialog.DEFAULT_LANGUAGE));
						//netTextField.setValue(values.get("ja"));
						//Info.display("result", values.get("en_us") + "-" + values.get("ko"));

					}
				});

		loginpDialog = new MultiLanguageDialog();
		loginpDialog.setAllowBlank(false);
		loginpDialog.addMultiLanguageDialogEventHandler(new MultiLanguageDialogEventHandler() {

					@Override
					public void onButtonOKClicked(
							MultiLanguageDialogOKEvent event) {
						// Get map language-text
						Map<String, String> values = event.getSource()
								.getLanguageTexts();

						// Display English in label
						loginpTextField.setValue(values.get(MultiLanguageDialog.DEFAULT_LANGUAGE));
					}
				});
		loginiDialog = new MultiLanguageDialog();
		loginiDialog.setAllowBlank(false);
		loginiDialog.addMultiLanguageDialogEventHandler(new MultiLanguageDialogEventHandler() {

					@Override
					public void onButtonOKClicked(
							MultiLanguageDialogOKEvent event) {
						// Get map language-text
						Map<String, String> values = event.getSource().getLanguageTexts();
						// Display English in label
						loginiTextField.setValue(values.get(MultiLanguageDialog.DEFAULT_LANGUAGE));
						//loginiTextField.setValue(values.get("ja"));
						//Info.display("result", values.get("en_us") + "-" + values.get("ko"));
					}
				});
		serviceDialog = new MultiLanguageDialog();
		serviceDialog.setAllowBlank(false);
		serviceDialog.addMultiLanguageDialogEventHandler(new MultiLanguageDialogEventHandler() {

					@Override
					public void onButtonOKClicked(
							MultiLanguageDialogOKEvent event) {
						// Get map language-text
						Map<String, String> values = event.getSource().getLanguageTexts();

						// Display English in label
						serviceTextField.setValue(values.get(MultiLanguageDialog.DEFAULT_LANGUAGE));
					}
				});
		// Edit icon for MultiLanguage
		final HighLightIconButton serviceButton = new HighLightIconButton(
				ShareGWTConstants.CSS_EDIT_SM_ICON,
				new SelectionListener<IconButtonEvent>() {
					@Override
					public void componentSelected(IconButtonEvent ce) {
						serviceDialog.show();
					}
				});
		// Edit icon for MultiLanguage
		final HighLightIconButton loginpButton = new HighLightIconButton(
				ShareGWTConstants.CSS_EDIT_SM_ICON,
				new SelectionListener<IconButtonEvent>() {
					@Override
					public void componentSelected(IconButtonEvent ce) {
						loginpDialog.show();
					}
				});
		// Edit icon for MultiLanguage
		final HighLightIconButton loginiButton = new HighLightIconButton(
				ShareGWTConstants.CSS_EDIT_SM_ICON,
				new SelectionListener<IconButtonEvent>() {
					@Override
					public void componentSelected(IconButtonEvent ce) {
						loginiDialog.show();
					}
				});
		// Edit icon for MultiLanguage
		final HighLightIconButton netButton = new HighLightIconButton(
				ShareGWTConstants.CSS_EDIT_SM_ICON,
				new SelectionListener<IconButtonEvent>() {
					@Override
					public void componentSelected(IconButtonEvent ce) {
						netDialog.show();
					}
				});
		HorizontalPanel net = new HorizontalPanel();
//		final MultiLanguageTextArea ta = new MultiLanguageTextArea();
//		ta.setDialogTitle("Sample text area");
//		ta.setFieldLabel("Sample text area");
//		ta.setAllowBlank(false);
		netTextField = new TextField<String>();
		netTextField.setId("InterfaceConfigPanel_levelTextField");
		netTextField.setWidth(600);
		netTextField.setReadOnly(true);
		netTextField.setStyleAttribute("marginRight", "5px");
		netTextField.setStyleAttribute(CServiceConstants.CSS_MARGINTOP, "5px");
		netTextField.setStyleAttribute(CServiceConstants.CSS_MARGINBUTTOM,
				"15px");
		netTextField.setValidator(new Validator() {
			@Override
			public String validate(Field<?> field, String value) {
				if (value != null && !value.trim().isEmpty()
						&& value.trim().length() > 255) {
					return ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(
							CServiceConstants.CSERVICE_CONSTANTS
									.webServiceURL(), 255);
				}
				return null;
			}
		});
		net.add(netTextField);
		net.add(netButton);
		Label logp = new Label(CServiceConstants.CSERVICE_CONSTANTS.loginprompt());
		//logp.setStyleAttribute("marginBottom", "5px");
		logp.setStyleAttribute(CServiceConstants.CSS_MARGINTOP, "10px");
		logp.setStyleAttribute(CServiceConstants.CSS_MARGINBUTTOM, "5px");

		HorizontalPanel loginp = new HorizontalPanel();
		Map<String, String> testValue = new HashMap<String, String>();
		testValue.put("en_us", "English");
		testValue.put("ja", "Japanese");

		loginpTextField = new TextField<String>();
		loginpTextField.setWidth(600);
		loginpTextField.setReadOnly(true);
		loginpTextField.setStyleAttribute("marginRight", "5px");
		loginpTextField.setStyleAttribute(CServiceConstants.CSS_MARGINTOP,"5px");
		loginpTextField.setStyleAttribute(CServiceConstants.CSS_MARGINBUTTOM,"15px");
		loginpTextField.setId("InterfaceConfigPanel_loginpTextField");
		loginpTextField.setValidator(new Validator() {
			@Override
			public String validate(Field<?> field, String value) {
				if (value != null && !value.trim().isEmpty()
						&& value.trim().length() > 255) {
					return ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(
							CServiceConstants.CSERVICE_CONSTANTS
									.webServiceURL(), 255);
				}
				return null;
			}
		});
		loginp.add(loginpTextField);
		loginp.add(loginpButton);
		Label logi = new Label(
				CServiceConstants.CSERVICE_CONSTANTS.logininvalid());
		logi.setStyleAttribute(CServiceConstants.CSS_MARGINTOP, "10px");
		HorizontalPanel logini = new HorizontalPanel();

		loginiTextField = new TextField<String>();
		loginiTextField.setWidth(600);
		loginiTextField.setReadOnly(true);
		loginiTextField.setStyleAttribute("marginRight", "5px");
		loginiTextField.setStyleAttribute(CServiceConstants.CSS_MARGINTOP,"5px");
		loginiTextField.setStyleAttribute(CServiceConstants.CSS_MARGINBUTTOM,"15px");
		loginiTextField.setId("InterfaceConfigPanel_loginiTextField");
		loginiTextField.setValidator(new Validator() {
			@Override
			public String validate(Field<?> field, String value) {
				if (value != null && !value.trim().isEmpty()
						&& value.trim().length() > 255) {
					return ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(
							CServiceConstants.CSERVICE_CONSTANTS
									.webServiceURL(), 255);
				}
				return null;
			}
		});
		logini.add(loginiTextField);
		logini.add(loginiButton);

		Label serv = new Label(CServiceConstants.CSERVICE_CONSTANTS.servicenotavailable());
		HorizontalPanel servp = new HorizontalPanel();
		serviceTextField = new TextField<String>();
		serviceTextField.setWidth(600);
		serviceTextField.setReadOnly(true);
		serviceTextField.setStyleAttribute("marginRight", "5px");
		serviceTextField.setStyleAttribute(CServiceConstants.CSS_MARGINTOP,"5px");
		serviceTextField.setId("InterfaceConfigPanel_serviceTextField");
		serviceTextField.setValidator(new Validator() {
			@Override
			public String validate(Field<?> field, String value) {
				if (value != null && !value.trim().isEmpty()
						&& value.trim().length() > 255) {
					return ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(
							CServiceConstants.CSERVICE_CONSTANTS
									.webServiceURL(), 255);
				}
				return null;
			}
		});
		servp.add(serviceTextField);
		servp.add(serviceButton);

		fieldSet1.add(lnet);
		fieldSet1.add(net);
		fieldSet1.add(logp);
		fieldSet1.add(loginp);
		fieldSet1.add(logi);
		fieldSet1.add(logini);
		fieldSet1.add(serv);
		fieldSet1.add(servp);
		panel.add(fieldSet1);


		reservationSearchDaysField = new TextField<String>();
		reservationSearchDaysField.setId("PropertyConfigPanel_Name_reservationSearchDaysField");
		reservationSearchDaysField.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS
						.reservationSearchDays()+ CoreConstants.REQURIED_FIELD_LABEL);
		// reservationSearchDaysField.setValidator(reservationSearchDaysValidator);
		reservationSearchDaysField.setAllowBlank(false);

		vp.add(panel);
		// / Nut Save
		FormButton saveButton = new FormButton(
		CoreConstants.CORE_CONSTANTS.save());
		saveButton.setId("AuthenConfigPanel_saveButton");
		saveButton.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "560px");
		saveButton.setSize(60, 30);
		saveButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				

				if (loginopTextField.getValue() == CoreConstants.CORE_CONSTANTS
						.username() + " " + CoreConstants.CORE_CONSTANTS.pwd()) {
					currentAuthenConf.setLoginOption(LoginOption.USERNAME_PASS
							.ordinal());
					currentAuthenConf.setUsername(eUsernameTextField.getValue());
					currentAuthenConf.setPassword(passwordTextField.getValue());
				} else{
					currentAuthenConf.setLoginOption(LoginOption.ACCESS_CODE
							.ordinal());
				//currentAuthenConf.setUsername(eUsernameTextField.getValue());
				currentAuthenConf.setPassword(passwordTextField.getValue());
				}
				if (precheckinTextField.getValue() == "Enabled") {
					currentAuthenConf.setPreCheckInEnable(true);
				} else {
				currentAuthenConf.setPreCheckInEnable(false);}
				currentAuthenConf.setLevel(levelTextField.getValue());
				saveAuthenConf(currentAuthenConf);
//				saveMultiLanguageMap(networkAccessNotAvailableConf,"NETWORK_ACCESS_NOT_AVAILABLE");
				saveMultiLanguageMap(networkAccessNotAvailableConf, MultiLanguageConstants.network_access_not_available);
				saveMultiLanguageMap(loginPrompt,"LOGIN_PROMPT");
				saveMultiLanguageMap(loginInvalid,"LOGIN_INVALID");
				saveMultiLanguageMap(serviceNotAvailablePrecheckin,"SERVICE_NOT_AVAILABLE_FOR_PRECHECKIN");
				
				// if(loginopTextField.isValid()
				// && eUsernameTextField.isValid()
				// && passwordTextField.isValid()){
				// CServiceCommonServiceController.getINSTANCE().getAuthen(
				// propCombox.getValue().getEntity().getId(),
				// new AsyncCallbackListener<AuthenticationConfig>(){
				// @Override
				// public void onLogin(String username) {
				// CServiceCommonServiceController.getINSTANCE().getAuthen(
				// propCombox.getValue().getEntity().getId(),this);
				// }
				//
				// @Override
				// public void onFailure(Throwable caught) {
				// MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.system(),
				// ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
				// CoreConstants.CORE_CONSTANTS.systemError(), null);
				// }
				//
				// @Override
				// public void onSuccess( AuthenticationConfig result) {
				// if(null == result){
				// currentAuthenConf = new AuthenticationConfig();
				// currentAuthenConf.setOrgId(SessionContext.getInstance().getOrgId());
				// currentAuthenConf.setPropertyId(propCombox.getValue().getEntity().getId());
				// }else{
				// currentAuthenConf = result;
				// }
				// //if(loginopTextField.getValue()){}
				// //currentAuthenConf.setLoginOption(loginopTextField.getValue());
				// currentAuthenConf.setUsername(eUsernameTextField.getValue());
				// currentAuthenConf.setPassword(passwordTextField.getValue());
				//
				// saveAuthenConf(currentAuthenConf);
				// }
				//
				// });
				// }

			}
		});
		vp.add(saveButton);

		// Get Map of Language
		// Keep in mine we must set Map of Languages for Dialog, TextField,
		// TextArea in method onSuccess
		CServiceCommonServiceController.getINSTANCE().getLanguageMap(
			new AsyncCallbackListener<Map<String, String>>() {

				@Override
				public void onLogin(String username) {
					CServiceCommonServiceController.getINSTANCE().getLanguageMap(this);
				}

				@Override
				public void onSuccess(Map<String, String> result) {
					final Map<String, String> languages = result;
					netDialog.setLanguages(languages);
					loginpDialog.setLanguages(languages);
					loginiDialog.setLanguages(languages);
					serviceDialog.setLanguages(languages);
				}

				@Override
				public void onFailure(Throwable caught) {
					Info.display("failure", caught.getMessage());
				}
			});

		
		// ket thuc khai bao Multilanguage
		add(vp);
		onShow();
	}

	@Override
	public UserAuthority getRequiredAuthority() {
		return CServiceAuthority.cSERVICE_CONFIG;
	}

	@Override
	protected Widget getViewPanel() {
		return vp;
	}

	@Override
	public void onShow() {
		loadProperty();
		myClear();

		String s = "Display String " + SessionContext.getInstance().getPropertyLanguage().getDisplayStr()
				+ "Language code " + SessionContext.getInstance().getPropertyLanguage().getLangCode();
		Window.alert(s);
		
		CServiceCommonServiceController.getINSTANCE().getAuthen(
			SessionContext.getInstance().getPropertyId(),SessionContext.getInstance().getOrgId(),
			new AsyncCallbackListener<AuthenticationConfig>() {

		@Override
		public void onLogin(String username) {
			CServiceCommonServiceController.getINSTANCE()
					.getAuthen(
							SessionContext.getInstance()
									.getPropertyId(),SessionContext.getInstance().getOrgId(), this);
		}

		@Override
		public void onFailure(Throwable caught) {
			MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(
					CoreConstants.CORE_CONSTANTS.system(),
					ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
					CoreConstants.CORE_CONSTANTS.systemError(),
					null);
		}

		@Override
		public void onSuccess(AuthenticationConfig result) {
			if (null != result) {
				currentAuthenConf = result;
				if (result.getLoginOption() == LoginOption.USERNAME_PASS.ordinal()) {
					loginopTextField.setValue(CoreConstants.CORE_CONSTANTS.username()+ " "
									+ CoreConstants.CORE_CONSTANTS.pwd());
				} else {
					loginopTextField.setValue(CServiceConstants.CSERVICE_CONSTANTS.accesscode());
					eUsernameTextField.hide();
					l2.hide();
				}
				eUsernameTextField.setValue(result.getUsername());
				passwordTextField.setValue(result.getPassword());
				if (result.getPreCheckInEnable()) {
					precheckinTextField.setValue("Enabled");
				} else
					precheckinTextField.setValue("Disabled");

				levelTextField.setValue(result.getLevel());
			}
		}

	});
//////Get Language for network access not available
		CServiceCommonServiceController.getINSTANCE().getListConstantsLanguage(SessionContext.getInstance().getPropertyId(), SessionContext.getInstance().getOrgId(), "NETWORK_ACCESS_NOT_AVAILABLE",
				new AsyncCallbackListener<List<ConstantsLanguageValue>>(){

			@Override
			public void onLogin(String username) {
				CServiceCommonServiceController.getINSTANCE().getListConstantsLanguage(SessionContext.getInstance().getPropertyId(),SessionContext.getInstance().getOrgId(),"NETWORK_ACCESS_NOT_AVAILABLE",this);	
				Window.alert("login success!");
			}

			@Override
			public void onFailure(Throwable caught) {
				MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.system(), ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
						CoreConstants.CORE_CONSTANTS.systemError(), null);
				Window.alert("get language fail!");
			}
		

			@Override
			public void onSuccess(
				List<ConstantsLanguageValue> result1) {
				networkAccessNotAvailableConf = convertToMap(result1);
				netTextField.setValue(networkAccessNotAvailableConf.get("en_us"));
				//netTextField.setValue(networkAccessNotAvailableConf.get(SessionContext.getInstance().getPropertyLanguage().getDisplayStr()));
				netDialog.setLanguageTexts(networkAccessNotAvailableConf);
			}
	});
//////Get Language for Login prompt
		CServiceCommonServiceController.getINSTANCE().getListConstantsLanguage(1, 1, "LOGIN_PROMPT",
				new AsyncCallbackListener<List<ConstantsLanguageValue>>(){

			@Override
			public void onLogin(String username) {
				CServiceCommonServiceController.getINSTANCE().getListConstantsLanguage(1,1,"LOGIN_PROMPT",this);	
				Window.alert("login success!");
			}

			@Override
			public void onFailure(Throwable caught) {
				MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.system(), ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
						CoreConstants.CORE_CONSTANTS.systemError(), null);
				Window.alert("get language fail!");
			}
		

			@Override
			public void onSuccess(
				List<ConstantsLanguageValue> result2) {
				loginPrompt = convertToMap(result2);
				loginpTextField.setValue(loginPrompt.get("en_us"));
				loginpDialog.setLanguageTexts(loginPrompt);
				
			}
	});
//////Get Language for Login Invalid
		CServiceCommonServiceController.getINSTANCE().getListConstantsLanguage(1, 1, "LOGIN_INVALID",
				new AsyncCallbackListener<List<ConstantsLanguageValue>>(){

			@Override
			public void onLogin(String username) {
				CServiceCommonServiceController.getINSTANCE().getListConstantsLanguage(1,1,"LOGIN_INVALID",this);	
				Window.alert("login success!");
			}

			@Override
			public void onFailure(Throwable caught) {
				MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.system(), ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
						CoreConstants.CORE_CONSTANTS.systemError(), null);
				Window.alert("get language fail!");
			}
		

			@Override
			public void onSuccess(
				List<ConstantsLanguageValue> result3) {				
				loginInvalid = convertToMap(result3);
				loginiTextField.setValue(loginInvalid.get("en_us"));
				loginiDialog.setLanguageTexts(loginInvalid);
				
			}
	});
//////Get Language for Service not available for precheckin
		CServiceCommonServiceController.getINSTANCE().getListConstantsLanguage(1, 1, "SERVICE_NOT_AVAILABLE_FOR_PRECHECKIN",
				new AsyncCallbackListener<List<ConstantsLanguageValue>>(){

			@Override
			public void onLogin(String username) {
				CServiceCommonServiceController.getINSTANCE().getListConstantsLanguage(1,1,"SERVICE_NOT_AVAILABLE_FOR_PRECHECKIN",this);	
				Window.alert("login success!");
			}

			@Override
			public void onFailure(Throwable caught) {
				MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.system(), ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
						CoreConstants.CORE_CONSTANTS.systemError(), null);
				Window.alert("get language fail!");
			}
		

			@Override
			public void onSuccess(
				List<ConstantsLanguageValue> result4) {
				serviceNotAvailablePrecheckin = convertToMap(result4);
				serviceTextField.setValue(serviceNotAvailablePrecheckin.get("ja"));
				serviceDialog.setLanguageTexts(serviceNotAvailablePrecheckin);
				
			}
	});
	}//Dong Onshow
//Convert from List<> to Map<string,string>
	public Map<String, String> convertToMap( List<ConstantsLanguageValue> listlang ){
		Map<String, String> resultmap = new HashMap<String, String>();
		for(ConstantsLanguageValue lang : listlang){
			resultmap.put(lang.getId().getLanguageId(), lang.getValue());
		}
		return resultmap;
		
	}
	@Override
	public boolean onHide() {
		myClear();
		return true;
	}

	private void myClear() {
		reservationSearchDaysField.clear();
	}
	
//Dinh nghia ham Save MultiLanguage
	private void saveMultiLanguageMap(Map<String, String> abc, String constants)
	{

		for (Entry<String, String> entry : abc.entrySet()) {
			//Window.alert("Key : " + entry.getKey() + " Value : " + entry.getValue());
			if(entry.getValue()!=null){
				final ConstantsLanguageValue multiconstants=new ConstantsLanguageValue();

//				SessionContext.getInstance().getPropertyId()
				//constants=new ConstantsLanguageValueId();
				multiconstants.setId(new ConstantsLanguageValueId(SessionContext.getInstance().getPropertyId(), SessionContext.getInstance().getOrgId(), constants, entry.getKey()));
				//multiconstants.setId(new ConstantsLanguageValueId(1, 1, "NETWORK_ACCESS_NOT_AVAILABLE", entry.getKey()));
				multiconstants.setValue(entry.getValue());
				multiconstants.setCreated(new Date());
				multiconstants.setUpdated(new Date());
				multiconstants.setCreatedBy(SessionContext.getInstance().getUserId());
				multiconstants.setUpdatedBy(SessionContext.getInstance().getUserId());
				
				CServiceCommonServiceController.getINSTANCE().saveConstantsLanguageValue(multiconstants,
						new AsyncCallbackListener<ConstantsLanguageValue>() {

							@Override
							public void onLogin(String username) {
								CServiceCommonServiceController.getINSTANCE()
										.saveConstantsLanguageValue(multiconstants, this);
							}

							@Override
							public void onFailure(Throwable caught) {
								MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(
										CoreConstants.CORE_CONSTANTS.system(),
										ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
										CoreConstants.CORE_CONSTANTS.systemError(),null);
							}

							@Override
							public void onSuccess(ConstantsLanguageValue result) {
								if (result != null) {
									CenteredInfo.displayCompleted(CoreConstants.CORE_CONSTANTS.edited());
								} else {
									MessageBox.alert(
											CoreConstants.CORE_MESSAGES.operation(
													CoreConstants.CORE_CONSTANTS.system(),
													ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
											CoreConstants.CORE_CONSTANTS.systemError(),null);
								}
							}
						});
			
			}//Dong if
		       
		}//Dong For
		
		//CServiceCommonServiceController.getINSTANCE().saveConstantsLanguageValue(constants, callback);
	}
//Ham Save iGuest Authentication
	private void saveAuthenConf(final AuthenticationConfig conf) {
		CServiceCommonServiceController.getINSTANCE().SaveAuthen(conf,
				new AsyncCallbackListener<AuthenticationConfig>() {

					@Override
					public void onLogin(String username) {
						CServiceCommonServiceController.getINSTANCE()
								.SaveAuthen(conf, this);
					}

					@Override
					public void onFailure(Throwable caught) {
						MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(
								CoreConstants.CORE_CONSTANTS.system(),
								ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
								CoreConstants.CORE_CONSTANTS.systemError(),
								null);
					}

					@Override
					public void onSuccess(AuthenticationConfig result) {
						if (result != null) {
							CenteredInfo.displayCompleted(CoreConstants.CORE_CONSTANTS.edited());
						} else {
							MessageBox.alert(
									CoreConstants.CORE_MESSAGES.operation(
										CoreConstants.CORE_CONSTANTS.system(),
										ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
									CoreConstants.CORE_CONSTANTS.systemError(),
									null);
						}
					}
				});
	}

	@Override
	public void setFocus() {

	}

	private void loadProperty() {
		propGroupIdNames = SessionContext.getInstance().getPropertyGroup();
		propListStore.removeAll();
		if (0 != propGroupIdNames.size()) {
			int propIndex = 0;
			for (IdNameModel<Property> inm : propGroupIdNames) {
				propListStore.add(new PropertyIdNameModelData(inm));
				if (propIndex == 0) {
					if (SessionContext.getInstance().getPropertyId() == inm
							.getId()) {
						propIndex = propListStore.getCount() - 1;
					}
				}
			}
			propCombox.setValue(propListStore.getAt(propIndex));
		} else {
			IdNameModel<Property> tempINM = new IdNameModel<Property>(
					SessionContext.getInstance().getPropertyId(),
					SessionContext.getInstance().getPropertyName());
			propListStore.add(new PropertyIdNameModelData(tempINM));
			propCombox.setValue(propListStore.getAt(0));
		}
	}

	public HighLightIconButton createEditBtn() {
		HighLightIconButton iconBtn = new HighLightIconButton(
				ShareGWTConstants.CSS_EDIT_SM_ICON,
				new SelectionListener<IconButtonEvent>() {
					@Override
					public void componentSelected(IconButtonEvent ce) {
						dialog.show();
					}
				});
		iconBtn.setTitle(CoreConstants.CORE_CONSTANTS.edit());
		iconBtn.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "5px");
		return iconBtn;
	}

	// / Pre checkin Edit Button
	public HighLightIconButton createPreEditBtn() {
		HighLightIconButton iconBtn = new HighLightIconButton(
				ShareGWTConstants.CSS_EDIT_SM_ICON,
				new SelectionListener<IconButtonEvent>() {
					@Override
					public void componentSelected(IconButtonEvent ce) {
						// predialog = PrecheckinDialog.getInstance();
						predialog.show();
					}
				});
		iconBtn.setTitle(CoreConstants.CORE_CONSTANTS.edit());
		iconBtn.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "5px");
		return iconBtn;
	}

	@Override
	public void onOkButton(DialogEvent e) {

		if ((e.getUsername() != null)
				&& (e.getOption() == LoginOption.USERNAME_PASS)) {

			loginopTextField.setValue(CoreConstants.CORE_CONSTANTS.username()
					+ " " + CoreConstants.CORE_CONSTANTS.pwd());
			l2.show();
			eUsernameTextField.show();
			eUsernameTextField.setValue(e.getUsername());
			eUsernameTextField.setValue(e.getUsername());
		} else {
			loginopTextField.setValue(CServiceConstants.CSERVICE_CONSTANTS
					.accesscode());
			l2.hide();
			eUsernameTextField.hide();

		}
		passwordTextField.setValue(e.getPassword());

	}

	@Override
	public void preOkButton(PrecheckinDialogEvent e) {
		// TODO Auto-generated method stub
		// Window.alert(e.getstatus());
		precheckinTextField.setValue(e.getstatus());
		levelTextField.setValue(e.getlevel());

	}

}
